﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Qing.Util {
    public class SecurityUtil {

        public static string DEFAULT_AES_IV = "lsxkEh8xsmMOfJSqWpfT6Q==";
        public static string DEFAULT_DES_IV = "drIjug5Sz44=";

        public static string AesEncrypt(string data, string key, string iv = "lsxkEh8xsmMOfJSqWpfT6Q==") {
            byte[] encrypted;

            using (Aes aesAlg = Aes.Create()) {
                aesAlg.Key = Convert.FromBase64String(key);
                aesAlg.IV = Convert.FromBase64String(iv);

                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream()) {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
                            swEncrypt.Write(data);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }

            return Convert.ToBase64String(encrypted);
        }

        public static string AesDecrypt(string data, string key, string iv = "lsxkEh8xsmMOfJSqWpfT6Q==") {
            string plaintext = null;

            // Create an Aes object
            // with the specified key and IV.
            using (Aes aesAlg = Aes.Create()) {
                aesAlg.Key = Convert.FromBase64String(key); 
                aesAlg.IV = Convert.FromBase64String(iv); 

                // Create a decryptor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(data))) {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt)) {

                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }

            return plaintext;

        }


        public static string DesEncrypt(string data, string key, string iv = "lsxkEh8xsmMOfJSqWpfT6Q==") {
            byte[] encrypted;

            using (DES desAlg = DES.Create()) {
                desAlg.Key = Convert.FromBase64String(key);
                desAlg.IV = Convert.FromBase64String(iv);

                ICryptoTransform encryptor = desAlg.CreateEncryptor(desAlg.Key, desAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream()) {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
                            swEncrypt.Write(data);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }

            return Convert.ToBase64String(encrypted);
        }

        public static string DesDecrypt(string data, string key, string iv = "lsxkEh8xsmMOfJSqWpfT6Q==") {
            string plaintext = null;

            // Create an Aes object
            // with the specified key and IV.
            using (DES desAlg = DES.Create()) {
                desAlg.Key = Convert.FromBase64String(key);
                desAlg.IV = Convert.FromBase64String(iv);

                // Create a decryptor to perform the stream transform.
                ICryptoTransform decryptor = desAlg.CreateDecryptor(desAlg.Key, desAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(data))) {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt)) {

                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }

            return plaintext;

        }

        public static string RsaEncrypt(string data, string key) {
            string encryptedContent = string.Empty;
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) {
                rsa.FromXmlString(key);
                byte[] encryptedData = rsa.Encrypt(Encoding.Default.GetBytes(data), false);
                encryptedContent = Convert.ToBase64String(encryptedData);
            }
            return encryptedContent;
        }

        public static string RsaDecrypt(string data, string key) {
            string decryptedContent = string.Empty;
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) {
                rsa.FromXmlString(key);
                byte[] decryptedData = rsa.Decrypt(Convert.FromBase64String(data), false);
                decryptedContent = Encoding.UTF8.GetString(decryptedData);
            }
            return decryptedContent;
        }






    }
}
